<template>
  <div class="asset-transfer-page">
    <el-card>
      <div style="margin-bottom: 16px; display: flex; justify-content: space-between; align-items: center;">
        <span style="font-weight: bold;">资产转移管理</span>
        <el-button type="primary" size="small" @click="onAdd">新增转移</el-button>
      </div>
      <el-table :data="list" border stripe style="width: 100%;">
        <el-table-column prop="id" label="ID" width="60" />
        <el-table-column prop="assetId" label="资产" min-width="120">
          <template #default="scope">
            {{ assetMap[scope.row.assetId] || '-' }}
          </template>
        </el-table-column>
        <el-table-column prop="fromLocation" label="原位置" min-width="120" />
        <el-table-column prop="toLocation" label="新位置" min-width="120" />
        <el-table-column prop="transferUser" label="操作人" min-width="100" />
        <el-table-column prop="transferTime" label="转移时间" min-width="160" />
        <el-table-column prop="remark" label="备注" min-width="120" />
        <el-table-column label="操作" width="120">
          <template #default="scope">
            <el-button size="small" type="danger" @click="onDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 新增弹窗 -->
    <el-dialog v-model="dialog.visible" title="新增转移" width="400px">
      <el-form :model="dialog.form" label-width="100px">
        <el-form-item label="资产" required>
          <el-select v-model="dialog.form.assetId" placeholder="请选择资产">
            <el-option v-for="item in assetList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="原位置" required>
          <el-input v-model="dialog.form.fromLocation" placeholder="请输入原位置" />
        </el-form-item>
        <el-form-item label="新位置" required>
          <el-input v-model="dialog.form.toLocation" placeholder="请输入新位置" />
        </el-form-item>
        <el-form-item label="操作人" required>
          <el-input v-model="dialog.form.transferUser" placeholder="请输入操作人" />
        </el-form-item>
        <el-form-item label="备注">
          <el-input v-model="dialog.form.remark" />
        </el-form-item>
      </el-form>
      <template #footer>
        <el-button @click="dialog.visible = false">取消</el-button>
        <el-button type="primary" @click="doSave">确定</el-button>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import request from '@/utils/request'

const list = ref([])
const assetList = ref([])
const assetMap = reactive({})
const fetchList = async () => {
  const res = await request.get('/asset/transfer/list')
  if (res.code === 1) {
    list.value = res.data
  }
}
const fetchAssetList = async () => {
  const res = await request.get('/asset/list')
  if (res.code === 1) {
    assetList.value = res.data
    res.data.forEach(item => { assetMap[item.id] = item.name })
  }
}
const dialog = reactive({ visible: false, form: { assetId: null, fromLocation: '', toLocation: '', transferUser: '', remark: '' } })
const onAdd = () => {
  dialog.form = { assetId: null, fromLocation: '', toLocation: '', transferUser: '', remark: '' }
  dialog.visible = true
}
const doSave = async () => {
  if (!dialog.form.assetId || !dialog.form.fromLocation || !dialog.form.toLocation || !dialog.form.transferUser) {
    ElMessage.warning('请填写完整信息')
    return
  }
  await request.post('/asset/transfer', dialog.form)
  ElMessage.success('新增成功')
  dialog.visible = false
  fetchList()
}
const onDelete = (row) => {
  ElMessageBox.confirm('确认删除该记录？', '提示', { type: 'warning' }).then(async () => {
    await request.delete(`/asset/transfer/${row.id}`)
    ElMessage.success('删除成功')
    fetchList()
  })
}
onMounted(() => {
  fetchAssetList()
  fetchList()
})
</script>

<style scoped>
.asset-transfer-page {
  padding: 24px;
}
</style> 